﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BizInterface.HR;
using Base.Core.Biz;
using Model.Custom;

namespace Presentation.Web.Pages.Employee
{
    public partial class EmployeeList : System.Web.UI.Page
    {
        public IEmployeeService EmployeeService
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadEmployees();
            }

        }

        protected void dgrEmployees_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                string id = e.CommandArgument as string;
                Response.Redirect("EmployeeEdit.aspx?" + Constants.Web.ID + "=" + id);
            }

            if (e.CommandName == "Delete")
            {
                Delete(e.CommandArgument as string);
                LoadEmployees();
            }
        }
        private void LoadEmployees()
        {
            try
            {
                IList<SearchInfo> searchInfos = CreateSearchInfos();
                IList<OrderInfo> orderInfos = new List<OrderInfo> { new OrderInfo() { Name = "Name", OrderType = OrderType.Asc } };

                var employees = EmployeeService.GetAllEmployees(searchInfos, null, orderInfos)
                                               .ToList();
                this.dgrEmployees.DataSource = employees;
                this.dgrEmployees.DataBind();
            }
            catch { }
        }

        private IList<SearchInfo> CreateSearchInfos()
        {
            IList<SearchInfo> searchInfos = new List<SearchInfo>();

            if (!String.IsNullOrEmpty(txtName.Text))
            {
                searchInfos.Add(new SearchInfo() { Name = "Name", Value = txtName.Text, SearchOperator = SearchOperator.Like });
            }

            return searchInfos;
        }

        private void Delete(string employeeID)
        {
            int id = Convert.ToInt32(employeeID);
            EmployeeService.DeleteEmployee(id);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("EmployeeEdit.aspx");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadEmployees();
        }

        protected void btnReport_Click(object sender, EventArgs e)
        {
            IList<SearchInfo> searchInfos = CreateSearchInfos();
            IList<OrderInfo> orderInfos = new List<OrderInfo> { new OrderInfo() { Name = "Name", OrderType = OrderType.Asc } };

            var employees = EmployeeService.GetAllEmployees(searchInfos, null, orderInfos)
                                           .ToList();

            EmployeeReportInfo reportInfo = new EmployeeReportInfo(employees, searchInfos);
            Session[Constants.Web.EmployeeListReportInfoKey] = reportInfo;

            Response.Redirect("EmployeeListReport.aspx");
        }
    }
}